<template>
  <Card style="height:900px">
    <span style="font-weight: bold;font-size:20px">还款计算方法</span>
    <Form ref="formInline" :model="formDatas" :label-width="800" :rules="ruleInline">
      <Card>
        <Row>
          <Col span="10" order="1" class="row-col-style3">机构</Col>
          <FormItem>
            <Select v-model="formDatas.organization" v-if="type==1||type==0" style="width: 300px;">
              <Option v-for="item in orgList" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
            <span v-text="getOptionValue(orgList,formDatas.organization)" v-if="type==2"></span>
          </FormItem>
        </Row>
        </br>
        <Row>
          <Col span="10" class="row-col-style3">描述</Col>
          <FormItem prop="description">
            <Input v-model="formDatas.description" style="width: 300px;" placeholder="请输入" v-if="type==1||type==0"></Input>
            <span v-text="formDatas.description" v-if="type==2"></span>
          </FormItem>
        </Row>
        </br>
        <Row>
          <Col span="10" class="row-col-style3">还款计算方式</Col>
          <FormItem>
            <Select v-model="formDatas.amortCode" style="width: 300px;" v-if="type==1||type==0">
              <Option v-for="item in amortCodeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
            <span v-text="getOptionValue(amortCodeList,formDatas.amortCode)" v-if="type==2"></span>
          </FormItem>
        </Row>
        </br>
        <Row>
          <Col span="10" class="row-col-style3">进位方法</Col>
          <FormItem>
            <Select v-model="formDatas.roundUp" style="width: 300px;" v-if="type==1||type==0">
              <Option v-for="item in roundUpList" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
            <span v-text="getOptionValue(roundUpList,formDatas.roundUp)" v-if="type==2"></span>
          </FormItem>
        </Row>
        </br>
        <Row>
          <Col span="10" class="row-col-style3">进位数</Col>
          <FormItem prop="roundDigit">
            <Input v-model="formDatas.roundDigit" style="width: 300px;" placeholder="请输入" v-if="type==1||type==0"></Input>
            <span v-text="formDatas.roundDigit" v-if="type==2"></span>
          </FormItem>
        </Row>
      </Card>
      </br>
      <span style="margin-top:20px;font-weight: bold;font-size:20px">更新信息</span>
      <Card>
        <Row type="flex">
          <Col span="8" order="1" class="row-col-style">最后更新时间</Col>
          <Col span="3" order="2" class="row-col-style2">{{formDatas.updatedDate?this.$util.loanModularFormatTime(formDatas.updatedDate):this.$util.loanModularFormatTime(new Date())}}</Col>
          <Col span="5" order="3" class="row-col-style">最后更新人员</Col>
          <Col span="3" order="4" class="row-col-style2">{{formDatas.updatedBy}}</Col>
        </Row>
        </br>
        <Row type="flex">
          <Col span="8" order="1" class="row-col-style">审核时间</Col>
          <Col span="3" order="2" class="row-col-style2">{{formDatas.checkTime?this.$util.loanModularFormatTime(formDatas.checkTime):''}}</Col>
          <Col span="5" order="3" class="row-col-style">审核人员</Col>
          <Col span="3" order="4" class="row-col-style2">{{formDatas.checkUid}}</Col>
        </Row>
        </br>
        <Row type="flex">
          <Col span="8" order="1" class="row-col-style">审核意见</Col>
          <Col span="3" order="2" class="row-col-style2">{{formDatas.checkDesc}}</Col>
        </Row>
        </br>
        <FormItem>
          <Row>
            <div class="bottom-btn" v-if="type==1||type==0">
              <Button type="primary" @click="saveOrUpdate()">确定</Button>
              <Button type="primary" @click=resetForm>重置</Button>
              <Button type="primary" @click="handleGoBack">返回</Button>
            </div>
            <div class="bottom-btn" v-else>
              <Button type="primary" v-show="formDatas.status==9" @click="changeTypeToEdit()">编辑</Button>
              <Button type="primary" @click="deleteBykey">删除</Button>
              <Button type="primary" @click="handleGoBack">返回</Button>
            </div>
          </Row>
        </FormItem>
      </Card>
    </Form>
  </Card>
</template>
<script>
const validateRoundDigit = (rule, value, callback) => {
  var myreg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/
  if (!myreg.test(value)) {
    callback(new Error('请输入正确的进位数'))
  } else {
    callback()
  }
}
export default {
  data () {
    return {
      $ajax: null,
      formDatas: {
        organization: '100',
        description: '',
        amortCode: '0001',
        roundUp: 0,
        roundDigit: '',
        status: ''
      },
      defaultFormData: {},
      ruleInline: {
        roundDigit: [
          {
            validator: validateRoundDigit,
            trigger: 'blur'
          }
        ],
        description: [{
          required: true,
          message: '描述不能为空',
          trigger: 'blur'
        }]
      },
      type: 0, // 1 编辑   0 新增     2 查看

      orgList: [{
        value: '100',
        label: '消费金融'
      }],

      amortCodeList: [{
        value: '0001',
        label: '按月结息,到期还本付息'
      },
      {
        value: '0002',
        label: '按季度结息,到期还本付息'
      },
      {
        value: '0003',
        label: '一次性还本付息'
      },
      {
        value: '0004',
        label: '等额本息'
      },
      {
        value: '0005',
        label: '等额本金'
      }
      ],

      roundUpList: [{
        value: 0,
        label: '四舍五入'
      },
      {
        value: 1,
        label: '进位'
      },
      {
        value: 2,
        label: '截位'
      }
      ]
    }
  },
  created () {
    this.$ajax = this.$util.ajax()
    this.initPage()
  },
  computed: {
    index () {
      return this.$route.query.index
    }
  },
  mounted () {

  },
  methods: {
    // 返回上一页
    handleGoBack () {
      this.$router.back()
    },

    handleRouterGo (path) {
      this.$router.push(path)
    },

    changeTypeToEdit () {
      this.type = 1
    },

    // 初始化页面
    initPage () {
      var type = this.$route.query.type
      this.type = type
      if (type === 1 || type === 2) {
        const vm = this
        vm.$ajax.get(vm.$appContext.marvels_core_product + '/product/param/amortmethod/service/getKey/' + vm.index).then(re => {
          var data = re.data
          vm.formDatas = data.data
          vm.defaultFormData = JSON.parse(JSON.stringify(data.data))
        }).catch(er => {

        })
      }
    },
    // 通用获取下拉框的文本信息
    getOptionValue (optionArray, keyValue) {
      for (var i = 0; i < optionArray.length; i++) {
        if (optionArray[i].value === keyValue) {
          return optionArray[i].label
        }
      }
      return ''
    },
    saveOrUpdate () {
      const vm = this
      this.$refs.formInline.validate(valid => {
        if (valid) {
          let params = vm.formDatas
          if (vm.type === 0) {
            // 保存
            this.$ajax.post(vm.$appContext.marvels_core_product + '/product/param/amortmethod/service/save', params).then(re => {
              var data = re.data
              var code = data.code
              var message = data.message
              if (code !== 'success') {
                this.$Message.success(message)
                return
              }
              alert('添加成功')
              // 返回列表页
              vm.$router.push({
                name: 'amortMethod'
              })
            }).catch(er => {

            })
          } else {
            // 更新
            this.$ajax.post(vm.$appContext.marvels_core_product + '/product/param/amortmethod/service/update', params).then(re => {
              var data = re.data
              var code = data.code
              var message = data.message
              if (code !== 'success') {
                this.$Message.success(message)
                return
              }
              alert('更新成功')
              // 返回列表页
              vm.$router.push({
                name: 'amortMethod'
              })
            }).catch(er => {

            })
          }
        }
      })
    },
    deleteBykey () {
      var param = {
        amortId: this.$route.query.index
      }
      this.$ajax.post(this.$appContext.marvels_core_product + '/product/param/amortmethod/service/delete', param).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code === 'success') {
          this.$Message.success(message)
        } else {
          this.$Message.warn(data.data)
        }
        // 返回列表页
        this.$router.push({
          name: 'amortMethod'
        })
      }).catch(er => {
      })
    },
    resetForm () {
      this.formDatas = JSON.parse(JSON.stringify(this.defaultFormData))
    }
  },
  props: {},
  watch: {
    $route (val) { }
  }
}
</script>

<style lang="less">
@import '../../../styles/common.less';
@import '../../../styles/table.less';
.row-col-style {
  color: #2d8cf0;
  font-family: 微软雅黑;
  font-size: 20px;
  text-align: right;
}
.row-col-style2 {
  text-align: center;
}
.row-col-style3 {
  font-family: 微软雅黑;
  font-size: 20px;
  text-align: right;
}
</style>
